package com.tally.business.controller;

import com.tally.server.domain.Cost;
import com.tally.server.domain.CostExample;
import com.tally.server.dto.CostDTO;
import com.tally.server.dto.IncomeDTO;
import com.tally.server.enums.CostTypeEnum;
import com.tally.server.enums.IncomeTypeEnum;
import com.tally.server.mapper.CostMapper;
import com.tally.server.mapper.IncomeMapper;
import com.tally.server.service.CostService;
import com.tally.server.service.IncomeService;
import com.tally.server.utils.DateUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private CostMapper costMapper;

    @Autowired
    private CostService costService;

    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private IncomeService incomeService;

    private Integer userId = 1;

    @GetMapping("/addCost")
    public void addCost() {
        // 每天添加的条数
        Random random = new Random();
        LocalDate localDate = LocalDate.of(2021,6,5);
        Date endDate = new Date();
        List<Date> dateList = DateUtil.dateList(DateUtil.localDate2Date(localDate), endDate);
        CostTypeEnum[] values = CostTypeEnum.values();
        dateList.forEach(date -> {
            int numsPerDay = random.nextInt(10);
            for (int i = 0; i < numsPerDay; i++) {
                int types = random.nextInt(11);
                String costType = values[types].getCode();
                BigDecimal money = new BigDecimal(random.nextInt(200));
                CostDTO costDTO = new CostDTO();
                costDTO.setUserId(userId);
                costDTO.setCostType(costType);
                costDTO.setDetail("test专用");
                costDTO.setMoney(money);
                costDTO.setRecordDate(date);
                costService.save(costDTO);
            }
        });
    }

    @GetMapping("/addIncome")
    public void addIncome() {
        // 每天添加的条数
        Random random = new Random();
        LocalDate localDate = LocalDate.of(2021,6,5);
        Date endDate = new Date();
        List<Date> dateList = DateUtil.dateList(DateUtil.localDate2Date(localDate), endDate);
        IncomeTypeEnum[] values = IncomeTypeEnum.values();
        dateList.forEach(date -> {
            int numsPerDay = random.nextInt(4);
            for (int i = 0; i < numsPerDay; i++) {
                int types = random.nextInt(5);
                String incomeType = values[types].getCode();
                BigDecimal money = new BigDecimal(random.nextInt(300));
                IncomeDTO incomeDTO = new IncomeDTO();
                incomeDTO.setUserId(userId);
                incomeDTO.setIncomeType(incomeType);
                incomeDTO.setDetail("test专用");
                incomeDTO.setMoney(money);
                incomeDTO.setRecordDate(date);
                incomeService.save(incomeDTO);
            }
        });
    }

    @GetMapping("/deleteCost")
    public void deleteCost() {
        CostExample costExample = new CostExample();
        Date date1 = DateUtil.localDate2Date(LocalDate.of(2021,4,1));
        Date date2 = DateUtil.localDate2Date(LocalDate.of(2021,5,17));
        costExample.createCriteria().andRecordDateBetween(date1,date2).andUserIdEqualTo(userId);
        costMapper.deleteByExample(costExample);
    }
}
